package com.meet.framework.manager;


import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;

import androidx.annotation.NonNull;

import com.meet.framework.utils.LogUtils;

import java.io.IOException;

/**
 * 媒体播放
 */
public class MediaPlayerManager {

    //播放
    public  static final int MEDIA_STATUS_PLAY=0;

    //暂停
    public  static final int MEDIA_STATUS_PAUSE=1;

    //停止
    public  static final int MEDIA_STATUS_STOP=2;
    //播放
    public  static  int MEDIA_STATUS=MEDIA_STATUS_STOP;

    private MediaPlayer mediaPlayer;

    public  static final int H_PROGRESS=1000;

    private OnMusicProgessListener onMusicProgessListener;


    /**
     * 计算歌曲的进度
     * 1、开始播放的时候就开始循环计算时长
     * 2、将进度计算结果对外抛出
     */
    private Handler hander=new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case H_PROGRESS:
                    if(onMusicProgessListener!=null){
                        //当前时长
                        int currentPostion=getCurrentPosition();
                        //百分比,当前播放/总时长
                        int percent=(int)(((float)currentPostion)/((float)getDuration())*100);
                        onMusicProgessListener.OnProgess(currentPostion,percent);
                        //延迟1s
                        hander.sendEmptyMessageDelayed(H_PROGRESS,1000);
                    }
                    break;
            }
            return false;
        }
    });

    public MediaPlayerManager(){
        mediaPlayer=new MediaPlayer();
    }


    /**
     * 播放
     * @param path
     */
    public void startPlay(AssetFileDescriptor path){
        try {
            mediaPlayer.reset();
            mediaPlayer.setDataSource(path);
            mediaPlayer.prepare();
            mediaPlayer.start();
            MEDIA_STATUS=MEDIA_STATUS_PLAY;
            hander.sendEmptyMessage(H_PROGRESS);
        } catch (IOException e) {
            LogUtils.e(e.toString());
            throw new RuntimeException(e);
        }
    }

    /**
     * 是否在播放
     * @return
     */
    public boolean isPlaying(){
        return mediaPlayer.isPlaying();
    }

    /**
     * 暂停播放
     * @param
     */
    public void pausePlay(){
       if(isPlaying()){
           mediaPlayer.pause();
           MEDIA_STATUS=MEDIA_STATUS_PAUSE;
           hander.removeMessages(H_PROGRESS);
       }
    }

    /**
     * 停止播放
     * @param
     */
    public void stopPlay(){
        mediaPlayer.stop();
        MEDIA_STATUS=MEDIA_STATUS_STOP;
        hander.removeMessages(H_PROGRESS);
    }

    /**
     * 获取当前位置
     */
    public int getCurrentPosition(){
        return mediaPlayer.getCurrentPosition();
    }


    /**
     * 获取当前总时长
     */
    public int getDuration(){
        return mediaPlayer.getDuration();
    }


    /**
     * 播放错误
     * @param listener
     */
    public void setOnErrorListener(MediaPlayer.OnErrorListener listener){
        mediaPlayer.setOnErrorListener(listener);
    }

    /**
     * 播放结束
     * @param listener
     */
    public void setsetOnCompletionListener(MediaPlayer.OnCompletionListener listener){
        mediaPlayer.setOnCompletionListener(listener);
    }

    /**
     * 播放进度
     */
    public  void setOnProgressListener(OnMusicProgessListener listener){
        onMusicProgessListener=listener;
    }

    public  interface OnMusicProgessListener{
        void OnProgess(int progress,int percent);
    }

    /**
     * 是否循环
     *
     * @param isLooping
     */
    public void setLooping(boolean isLooping) {
        mediaPlayer.setLooping(isLooping);
    }

    /**
     * 继续播放
     */
    public void continuePlay() {
        mediaPlayer.start();
        MEDIA_STATUS = MEDIA_STATUS_PLAY;
        hander.sendEmptyMessage(H_PROGRESS);
    }


//
//    }
}
